//****************************************************************************************
//*
//* File Name: uart_buffer.c 
//* Level: bsp_UART
//* Version: v4.0
//* Date: 2024-07-09 13:05:10
//* Author: su
//* 
//****************************************************************************************/

//******************************** Includes *********************************//
#include <string.h>
#include <stdint.h>

#include "cmsis_os.h"
#include "queue.h"
#include "portmacro.h"

#include "uarttask.h"
#include "hal_uart.h"
#include "uart_buffer.h"
#include "elog.h"

//******************************** code *********************************//
uartdata_t *handleruart; //接收数据信息的总控     
uint8_t datastateflag;
QueueHandle_t xQueue;
QueueHandle_t packqueue;
void USART2_IRQHandler(void)
{
	BaseType_t pxHigherPriorityTaskWoken = pdFALSE;
	if(__HAL_UART_GET_FLAG(&huart2, UART_FLAG_IDLE))       //获取空闲中断
	{
		
		__HAL_UART_CLEAR_IDLEFLAG(&huart2);
        /*清除标志位，这块有大坑不要用__HAL_UART_CLEAR_FLAG(&huart2, UART_FLAG_IDLE)
		根据手册描述应该先读SR再度DR寄存器才可以清除*/
		HAL_UART_DMAStop(&huart2);	           //停止DMA
		(handleruart->length) = (MAX - __HAL_DMA_GET_COUNTER(&DMA_Init_Handle_uart_rx));  //获取长度
		datastateflag = receive_header_frame(handleruart);                      //获取状态也可以称为数据是否合法不合规就丢弃
		if(datastateflag != DATAOK)     //如果不合规直接强制数据清0，并且获取下一个buff地址存储，然后开启dma接收
		{
			memset(handleruart->payloadBuffer,0,handleruart->length);            
//			handleruart->payloadBuffer = get_nextbuffer_address();              
			HAL_UART_Receive_DMA(&huart2,handleruart->payloadBuffer, MAX);
		}else
		{  //合规数据发送给用户任务拆解
			xQueueSendToBackFromISR( xQueue,handleruart, &pxHigherPriorityTaskWoken );	
		}			
	}
	
		HAL_UART_IRQHandler(&huart2);	   
       portYIELD_FROM_ISR(pxHigherPriorityTaskWoken);

}

void uartthreadA(void *para)
{
	sendpack_t datapacket;         //接收拆包后数据的结构体
	uint32_t temp = 0;             //初始化数据解析命令
	while(1)
	{
		vTaskDelay(100);
		xQueueReceive(packqueue,&datapacket,TIME_WAITMAX);  //开启接收
		for(uint8_t i = 0;i<datapacket.length;i++) //循环获取数据累加
		{
//			uartprintf("datapacket[%d]  = %d\r\n",i,*(datapacket.Buffer_address + i));
			temp += *(datapacket.Buffer_address + i);
		}
		uartprintf("temp = %d\r\n",temp);
		if(temp == TEST_OUTPUT)  //如果数据正确 执行打印Decryption successful
		{
			uartprintf("Decryption successful\r\n");
			
		}
		temp = 0;  //清零计数
	}
}

void uartthreadB(void *para)
{
	bufferinit();                  //初始化buff最初地址
	handleruart = getbuffer();     //获取uart_buffer地址
	xQueue = xQueueCreate( 1, sizeof(uartdata_t)); //创建bufferhandler队列
	packqueue = xQueueCreate( 5, sizeof(sendpack_t));//创建解包后的数据
	HAL_UART_Receive_DMA(&huart2,handleruart->payloadBuffer,MAX);  //开启接收
	uartdata_t tempuartdata;      //实例化数据接收
	sendpack_t *tempsendpack;     //拆包后的数据包只有长度和数据
	while(1)
	{
		xQueueReceive(xQueue,&tempuartdata,TIME_WAITMAX);         //接收队列
		tempsendpack = parsingpack(&tempuartdata);                //指向解包后的数据
		xQueueSendToBack( packqueue,tempsendpack,TIME_NOWAIT);    //发送给需要数据的线程任务
		handleruart->payloadBuffer = get_nextbuffer_address();    //指向下有一个地址接收数据
		HAL_UART_Receive_DMA(&huart2,handleruart->payloadBuffer, MAX);      //开启接收
		vTaskDelay(10);
		
	}
}

void DMA1_Stream5_IRQHandler(void)
{

	HAL_DMA_IRQHandler(&DMA_Init_Handle_uart_rx);
}
void DMA1_Stream6_IRQHandler(void)
{

	HAL_DMA_IRQHandler(&DMA_Init_Handle_uart_tx);
}
//end uarttask
